import json

FILE_NAME = 'persons.json'
WEIGHT = 'weight'
HEIGHT = 'height'
HEALTH = 'health'


def write_json(data, file=FILE_NAME):
    with open(file, "w", encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, separators=(',', ':'))


def read_json(file=FILE_NAME):
    with open(file, "r", encoding='utf-8') as f:
        return json.load(f)


def judge_health(weight, height):
    BMI = float(weight / (height ** 2))
    if BMI < 18.5:
        return '低体重'
    elif BMI >= 18.5 and BMI < 24:
        return '正常'
    elif BMI >= 24 and BMI < 28:
        return '超重'
    else:
        return '肥胖'


def create_sample_input():
    d = []
    for i in range(5):
        d.append({'weight': 50, 'height': 170})
    write_json(d)


def calc_and_update(file=FILE_NAME):
    d = read_json(file)
    # print(d)
    for p in d:
        result = judge_health(p[WEIGHT], p[HEIGHT])
        p[HEALTH] = result
    write_json(d)


if __name__ == "__main__":
    # h = judge_health(10, 20)
    # print(h)
    # create_sample_input()
    calc_and_update()
